package com.wanban.download.utils

import android.annotation.SuppressLint

object DownloadConstants {
    const val INTENT_KEY_SERVICE_TASK = "task"
    const val INTENT_KEY_SERVICE_ACTION = "action"
    const val INTENT_KEY_SERVICE_DOWNLOAD_URL = "download_url"

    const val ERROR_HTTP_REQUEST_ERROR = "URL请求出错！"
    const val ERROR_CANNOT_DOWNLOAD_TO_DELETE = "无法下载，请删除后重试！"
    const val ERROR_FILE_LENGTH = "文件大小获取失败！"
    const val ERROR_DOWNLOAD_FILE_CREATE = "下载文件创建失败！"
    const val ERROR_TS_FILE_CREATE = "Ts目录文件创建失败！"
    const val ERROR_NETWORK_NO_CONNECT = "无网络链接，请检查您的网络！"
    const val ERROR_URL_NULL = "URL为空，无法下载！"
    const val ERROR_DOWNLOAD_MAX_QUEUE = "已达到最大下载数量，请等“下载中”列表的剧集下载完成后，再添加下载，谢谢！"
    const val ERROR_ONLY_WIFI = "不允许使用流量下载！"
    const val ERROR_DOWNLOAD_IN_QUEUE = "任务已在队列中，无法再次添加！"
    const val ERROR_DOWNLOAD_ADDED = "任务已存在，无法再次添加！"
    const val ERROR_URL_PARSE = "URL解析失败，无法下载！"
    const val ERROR_DOWNLOAD_UNKNOWN = "出现未知错误，无法下载！"


    const val SERVICE_TITLE = "下载服务"
    const val SERVICE_CONTENT = "正在下载中..."

    @SuppressLint("DefaultLocale")
    fun byte2FitMemorySize(byteSize: Long, precision: Int): String {
        require(precision >= 0) { "precision shouldn't be less than zero!" }
        return if (byteSize < 0) {
            throw IllegalArgumentException("byteSize shouldn't be less than zero!")
        } else if (byteSize < 1024) {
            String.format("%." + precision + "fB", byteSize.toDouble())
        } else if (byteSize < 1048576) {
            String.format("%." + precision + "fKB", byteSize.toDouble() / 1024)
        } else if (byteSize < 1073741824) {
            String.format("%." + precision + "fMB", byteSize.toDouble() / 1048576)
        } else {
            String.format("%." + precision + "fGB", byteSize.toDouble() / 1073741824)
        }
    }
}